package cn.com.agree.bigdata.vmdatahandle;


import cn.com.agree.bigdata.vmdatahandle.config.AppConfig;
import cn.com.agree.bigdata.vmdatahandle.config.AppConfigUtil;
import cn.com.agree.bigdata.vmdatahandle.config.YamlParser;
import cn.com.agree.bigdata.vmdatahandle.constant.GlobalConst;
import cn.com.agree.bigdata.vmdatahandle.service.ReadExporterWriteVm;
import cn.com.agree.bigdata.vmdatahandle.utils.QuartzManagerWithJob;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.*;

/**
 * @description: Afa Log Es Aging
 * @author: LiYuan
 * @version: 1.0
 * @create: 2019-06-20
 **/
@Slf4j
public class MyApplication {
    private static AppConfig appConfig;


    public static void main(String[] args) throws Exception {

        CommandLine cmd = commandLineParse(args);
        String cfgFile = cmd.getOptionValue('c');

        appConfig = YamlParser.getAppConfig(cfgFile);
        AppConfigUtil.setAppConfig(appConfig);
        log.info("appConfig:{}", appConfig);

        ReadExporterWriteVm readExporterWriteVm = new ReadExporterWriteVm();
        readExporterWriteVm.run();

    }

    public static AppConfig getAppConfig() {
        return appConfig;
    }


    /**
     * 命令行解析
     *
     * @param args
     * @return
     */
    private static CommandLine commandLineParse(String[] args) {
        Options options = new Options();

        final String configOptStr = "c";
        Option configOpt = new Option(configOptStr, "config", true, "yml config file");
        configOpt.setRequired(true);
        options.addOption(configOpt);

        final String versionOpt = "v";
        Option version = new Option(versionOpt, "version", false, "show version");
        version.setRequired(false);
        options.addOption(version);

        CommandLineParser parser = new DefaultParser();
        HelpFormatter helpFormatter = new HelpFormatter();
        CommandLine cmd = null;

        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            helpFormatter.printHelp(args[0], options);

            System.exit(1);
        }

        if (cmd.hasOption(versionOpt)) {
            System.out.println(GlobalConst.APP_VERSION);
            System.exit(0);
        }
        return cmd;
    }

}
